using System;
using System.Collections.Generic;
using Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter;
using System.IO;

public partial class ProviderLogin : System.Web.UI.Page
{
    Practice _providerPractice;
    List<Practice> _practiceCollection = new List<Practice>();
    List<Users> _usersCollection = new List<Users>();

    protected void Page_Load(object sender, EventArgs e)
    {

        AddJavaScript();
        if (!IsPostBack)
        {
            Session.Abandon();
            _providerPractice = new Practice(Path.Combine(Server.MapPath(@"Data"), Constants.PracticeXmlFileName));
            List<String> practiceNameCollection = new List<string>();
            _providerPractice = new Practice(Path.Combine(Server.MapPath(@"Data"), Constants.PracticeXmlFileName));
            _practiceCollection = _providerPractice.SelectPracticeRecords(string.Empty);

            foreach (Practice practice in _practiceCollection)
            {
                practiceNameCollection.Add(practice.PracticeName);
            }

            DropDownListProviderPractice.DataSource = practiceNameCollection;
            DropDownListProviderPractice.DataBind();

        }
    }
    protected void ButtonSignIn_Click(object sender, EventArgs e)
    {
        string practiceXmlPath = @"Data\Practices.xml";
        Users users = new Users(Server.MapPath(practiceXmlPath));

        _providerPractice = new Practice(Server.MapPath(practiceXmlPath));
        _practiceCollection = _providerPractice.SelectPracticeRecords("PracticeName='" + DropDownListProviderPractice.SelectedValue.ToString() + "'");

        if (!_practiceCollection.Count.Equals(0))
        {
            _providerPractice = _practiceCollection[0];
            _usersCollection = users.SelectUsersRecords("username='" + TextBoxUsername.Text +
                "' and password='" + TextBoxPassword.Text + "' and PracticeId='"
                + _practiceCollection[0].PracticeId.ToString() + "'");
            LiteralErrorMessage.Text = string.Empty;
            if (!_usersCollection.Count.Equals(0))
            {
                Session["UserName"] = TextBoxUsername.Text;
                Session["PracticeFolderName"] = _providerPractice.DataFolder;
                Session["ApplicationId"] = _providerPractice.ChildAppId;
                Session["providerPracticeName"] = DropDownListProviderPractice.SelectedValue.Trim();
                Response.Redirect("default.aspx", false);
            }
            else
            {
                LiteralErrorMessage.Text = "Invalid username or password";
            }
        }
    }
    protected void LinkButtonCreateUser_Click(object sender, EventArgs e)
    {
        Response.Redirect("CreateUser.aspx", false);
    }
    protected void LinkButtonNewRegistration_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProviderRegistration.aspx", false);
    }

    /// <summary>
    /// Add Java Scripts in Current page controls. 
    /// </summary>   
    private void AddJavaScript()
    {
        TextBoxUsername.Focus();
        TextBoxUsername.Attributes.Add("Onkeypress", "CheckSpecialCharacter('<,>,&,\",(,),^,!,#,$,%,*,+,=,;,:,{,}')");
        TextBoxPassword.Attributes.Add("Onkeypress", "CheckSpecialCharacter('<,>,\",(,),%,+,=,;,:,{,}')");
    }
}